package auth

import (
	"context"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"
	v1 "gitee.com/liuxuezhan/ar-platform/rpc-client/authRoleClient/api/role/v1"

	"github.com/zeromicro/go-zero/core/logx"
)

type CreateMenuLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// NewCreateMenuLogic 创建菜单
func NewCreateMenuLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateMenuLogic {
	return &CreateMenuLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *CreateMenuLogic) CreateMenu(req *types.CreateMenuRequest) (resp *types.CreateMenuResponse, err error) {
	menu, err := l.svcCtx.AuthRoleRpc.CreateMenu(l.ctx, &v1.CreateMenuRequest{
		MenuPid:      req.MenuPid,
		MenuName:     req.MenuName,
		MenuType:     v1.MenuType(req.MenuType),
		Target:       v1.MenuTarget(req.Target),
		Url:          req.Url,
		Icon:         req.Icon,
		Identifier:   req.Identifier,
		ListOrder:    req.ListOrder,
		Remark:       req.Remark,
		Visible:      v1.MenuVisible(req.Visible),
		Refresh:      v1.MenuRefresh(req.Refresh),
		Status:       v1.Status(req.Status),
		MenuLanguage: req.MenuLanguage,
	})

	if err != nil {
		return nil, err
	}

	return &types.CreateMenuResponse{
		Id:         menu.GetId(),
		MenuPid:    menu.GetMenuPid(),
		MenuName:   menu.GetMenuName(),
		MenuType:   int8(menu.GetMenuType()),
		Target:     int8(menu.GetTarget()),
		Url:        menu.GetUrl(),
		Icon:       menu.GetIcon(),
		ListOrder:  menu.GetListOrder(),
		Remark:     menu.GetRemark(),
		Status:     int8(menu.GetStatus()),
		Identifier: menu.GetIdentifier(),
		Visible:    int8(menu.GetVisible()),
		Refresh:    int8(menu.GetRefresh()),
		CreatedAt:  menu.GetCreatedAt(),
		UpdatedAt:  menu.GetUpdatedAt(),
	}, err
}
